const getTheme = () => {
  const paths = require('../paths');
  const path = require('path');
  const cpaasConfig = paths.getCpaasBootConfig(paths.appPath) || {};
  let externalTheme = {};
  try {
    let themeConfigFile =
      cpaasConfig.themeConfigFile || `${paths.appConfig}/theme`;
    if (typeof cpaasConfig.theme === 'string') {
      themeConfigFile = cpaasConfig.theme;
    }
    try {
      themeConfigFile = require.resolve(
        path.resolve(paths.appRootPath, themeConfigFile)
      );
    } catch {
      themeConfigFile = null;
    }
    if (themeConfigFile) {
      externalTheme = {
        ...require(themeConfigFile),
      };
    }
    if (typeof cpaasConfig.theme === 'object') {
      externalTheme = {
        ...externalTheme,
        ...cpaasConfig.theme,
      };
    }
  } catch (e) {
    console.error(e);
    process.exit();
  }
  return externalTheme;
};

module.exports = getTheme;
